package com.lnking.lnkingplace.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.mapper.HistoryDateConfigMapper;
import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.mapper.IsolationPlaceStatisticsMapper;
import com.lnking.lnkingplace.model.dto.isolation.place.PlaceReserveDto;
import com.lnking.lnkingplace.model.dto.isolation.statistics.HistoryDataDto;
import com.lnking.lnkingplace.model.dto.isolation.statistics.IsolationPlaceStatisticsCount;
import com.lnking.lnkingplace.model.entity.HistoryDateConfig;
import com.lnking.lnkingplace.model.enums.RoomType;
import com.lnking.lnkingplace.model.request.isolation.statistics.StatisticsSearchPersonnelType;
import com.lnking.lnkingplace.service.IIsolationPlaceStatisticsService;
import com.lnking.starter.mybatis.entity.IPageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class IsolationPlaceStatisticsServiceImpl implements IIsolationPlaceStatisticsService {

    @Resource
    IsolationPlaceStatisticsMapper isolationPlaceStatisticsMapper;
    @Resource
    HistoryDateConfigMapper historyDateConfigMapper;

    @Value("${epidemic.startDate}")
    String startDate;

    @Resource
    IsolationPlaceMapper isolationPlaceMapper;

    @Override
    public Map<String, Integer> getIsolationPersonnel() {
        Map<String,Integer> data = new HashMap<>();
        data.put("allNumber",isolationPlaceStatisticsMapper.getPersonCount(null));
        data.put("todayNumber",isolationPlaceStatisticsMapper.getPersonCount("now"));
        return data;
    }

    @Override
    public Map<String, Integer> getIsolationRoom(String street) {
        Map<String,Integer> data = new HashMap<>();
        data.put("allRoom",isolationPlaceStatisticsMapper.getRoomCount(null,street));
        data.put("useRoom",isolationPlaceStatisticsMapper.getRoomCount(RoomType.USEING,street));
        data.put("unusedRoom",isolationPlaceStatisticsMapper.getRoomCount(RoomType.UNUSE,street));
        data.put("engineeringRoom",isolationPlaceStatisticsMapper.getRoomCount(RoomType.ENGINEERING_ROOM,street));
        data.put("dirtyRoom",isolationPlaceStatisticsMapper.getRoomCount(RoomType.DIRTY_ROOM,street));
        return data;
    }

    @Override
    public Map<String, Integer> getIsolationWorker(String placeName,String street) {
        Map<String,Integer> data = new HashMap<>();
        data.put("allWorker",isolationPlaceStatisticsMapper.getWorkerCount(null,placeName,street));
        data.put("lineWorker",isolationPlaceStatisticsMapper.getWorkerCount("集中隔离区",placeName,street));
        data.put("twoLineWorker",isolationPlaceStatisticsMapper.getWorkerCount("行政保障区",placeName,street));
        return data;
    }

    @Override
    public Map<String, Integer> getIsolationAssistance(String street) {
        Map<String,Integer> data = new HashMap<>();
        data.put("allNumber",isolationPlaceStatisticsMapper.getAssistanceCount(null,street));
        data.put("resolvedNumber",isolationPlaceStatisticsMapper.getAssistanceCount("已处理",street));
        return data;
    }

    @Override
    public List<IsolationPlaceStatisticsCount> getIsolationAssistanceType(String street) {
        return isolationPlaceStatisticsMapper.getIsolationAssistanceType(street);
    }

    @Override
    public Map<String, Integer> getPlaceProblem(String street) {
        Map<String,Integer> data = new HashMap<>();
        data.put("allProblem",isolationPlaceStatisticsMapper.getPlaceProblem(null,null,null,street));
        data.put("RectificationProblem",isolationPlaceStatisticsMapper.getPlaceProblem("已整改",null,null,street));
        data.put("todayNewProblem",isolationPlaceStatisticsMapper.getPlaceProblem(null,"today",null,street));
        data.put("todayRectificationProblem",isolationPlaceStatisticsMapper.getPlaceProblem(null,null,"rectification",street));
        return data;
    }

    @Override
    public List<HistoryDataDto> getHistoryData(String street) {
        HistoryDateConfig historyDateConfig = historyDateConfigMapper.selectOne(null);
        String historyDate = LocalDateTimeUtil.localDateToString(historyDateConfig.getHistoryDate(),"yyyy");
        String yearDate = LocalDateTimeUtil.localDateToString(historyDateConfig.getYearDate(),"yyyy");
        String epidemicDate = LocalDateTimeUtil.localDateToString(historyDateConfig.getEpidemicDate(),"yyyy-MM-dd");

        List<HistoryDataDto> result = new ArrayList<>();
        HistoryDataDto first = new HistoryDataDto();
        first.setDate(historyDate);
        first.setAll(isolationPlaceStatisticsMapper.getHistoryData(historyDate,null,street));
        first.setPositive(isolationPlaceStatisticsMapper.getHistoryPositiveData(historyDate,null,street));
        first.setAfterPositive(isolationPlaceStatisticsMapper.getAfterPositive(historyDate,null,street));
        result.add(first);
        HistoryDataDto second = new HistoryDataDto();
        second.setDate(yearDate);
        second.setAll(isolationPlaceStatisticsMapper.getHistoryEqData(yearDate,street));
        second.setPositive(isolationPlaceStatisticsMapper.getHistoryEqPositiveData(yearDate,street));
        second.setAfterPositive(isolationPlaceStatisticsMapper.getAfterPositiveEq(yearDate,street));
        result.add(second);
        HistoryDataDto third = new HistoryDataDto();
        third.setDate(LocalDateTimeUtil.localDateToString(historyDateConfig.getEpidemicDate(),"MM.dd"));
        third.setAll(isolationPlaceStatisticsMapper.getHistoryData(null,epidemicDate,street));
        third.setPositive(isolationPlaceStatisticsMapper.getHistoryPositiveData(null,epidemicDate,street));
        third.setAfterPositive(isolationPlaceStatisticsMapper.getAfterPositive(null,epidemicDate,street));
        result.add(third);
        return result;
    }

    @Override
    public List<IsolationPlaceStatisticsCount> getPersonnelPieChart(String today,String street) {
        List<String> types = Arrays.asList("密接", "次密", "入境","社区排查");
        List<IsolationPlaceStatisticsCount> data = isolationPlaceStatisticsMapper.getPersonnelPieChart(today, street);
        List<IsolationPlaceStatisticsCount> result = new ArrayList<>();
        Integer otherNumber = 0;
        for (IsolationPlaceStatisticsCount info:data) {
            if(types.contains(info.getName())){
                result.add(info);
            }
            else {
                otherNumber += Integer.parseInt(info.getCount());
            }
        }
        IsolationPlaceStatisticsCount other = new IsolationPlaceStatisticsCount();
        other.setName("其他");
        other.setCount(String.valueOf(otherNumber));
        result.add(other);
        return result;
    }

    @Override
    public Map<String,Integer> getPositive(String street) {
        Map<String,Integer> data = new HashMap<>();
        data.put("positive",isolationPlaceStatisticsMapper.getPositive(startDate,street));
        return data;
    }

    @Override
    public List<IsolationPlaceStatisticsCount> getPositivePieChart(String street) {

        return isolationPlaceStatisticsMapper.getPositivePieChart(street);
    }

    @Override
    public List<IsolationPlaceStatisticsCount> getAfterPositivePieChart(String street) {
        return isolationPlaceStatisticsMapper.getAfterPositivePieChart(street);
    }

    @Override
    public List<IsolationPlaceStatisticsCount> getIsolationWorkerPieChart(String placeName, String camp,String street) {
        return isolationPlaceStatisticsMapper.getIsolationWorkerPieChart(placeName,camp,street);
    }

    @Override
    public List<IsolationPlaceStatisticsCount> getSupportOccupancyTotal(String placeName,String street) {
        return isolationPlaceStatisticsMapper.getSupportOccupancyTotal(placeName,street);
    }

    @Override
    public List<IsolationPlaceStatisticsCount> getSupportOccupancyNumber(String placeName,String street) {
        return isolationPlaceStatisticsMapper.getSupportOccupancyNumber(placeName,street);
    }

    @Override
    public List<IsolationPlaceStatisticsCount> getSupportDayNewNumber(String placeName,String street) {
        return isolationPlaceStatisticsMapper.getSupportDayNewNumber(placeName,street);
    }

    @Override
    public List<IsolationPlaceStatisticsCount> getSupportUseRoom(String placeName,String street) {
        return isolationPlaceStatisticsMapper.getSupportUseRoom(placeName,street);
    }

    @Override
    public Map<String, Integer> getPlacePlan(String street) {
        Map<String,Integer> data = new HashMap<>();
        data.put("medicalPlace",isolationPlaceStatisticsMapper.getMedicalPlace(street));
        data.put("medicalRoomNumber",isolationPlaceStatisticsMapper.getMedicalRoomNumber(street));
        data.put("medicalUseRoom",isolationPlaceStatisticsMapper.getMedicalUseRoom(street));
        data.put("supportPlace",isolationPlaceStatisticsMapper.getSupportPlace(street));
        data.put("supportUseRoom",isolationPlaceStatisticsMapper.getSupportUseRoomNumber(street));
        return data;
    }

    @Override
    public List<PlaceReserveDto> getIsolationPlaceReserve(String street) {
        return isolationPlaceStatisticsMapper.getIsolationPlaceReserve(street);
    }

    @Override
    public PlaceReserveDto getPlaceReserve(String placeType,String street) {
        return isolationPlaceStatisticsMapper.getPlaceReserve(placeType,street);
    }

    @Override
    public List<IsolationPlaceStatisticsCount> getLineChart(String type,String street) {
        List<IsolationPlaceStatisticsCount> result = null;

        //获取7天时间
        List<String> dates = isolationPlaceStatisticsMapper.getDate();

        switch (type){
            //在住隔离人员总数
            case "personnelTotal":
                result = getResult(null,street,dates);
                break;
            //密接人员
            case "contactPersonnel":
                result = getResult("密接",street,dates);
                break;
            //次密接人员
            case "secondaryContactPersonnel":
                result = getResult("次密",street,dates);
                break;
            //已用隔离房间数
            case "useRoom":
                result = getRoomResult(street,dates);
                break;
            //入境隔离
            case "entryQuarantine":
                result = getResult("入境",street,dates);
                break;
        }
        return result;
    }

    private List<IsolationPlaceStatisticsCount> getResult(String personnelType,String street,List<String> dates){
        List<IsolationPlaceStatisticsCount> result = new ArrayList<>();
        for(String date : dates){
            Integer personnelTotal = isolationPlaceStatisticsMapper.getPersonnelTotal(personnelType, street, date);
            IsolationPlaceStatisticsCount data = new IsolationPlaceStatisticsCount();
            data.setName(date);
            data.setCount(String.valueOf(personnelTotal));
            result.add(data);
        }
        return result;
    }

    private List<IsolationPlaceStatisticsCount> getRoomResult(String street,List<String> dates){
        List<IsolationPlaceStatisticsCount> result = new ArrayList<>();
        for(String date : dates){
            Integer personnelTotal = isolationPlaceStatisticsMapper.getUseRoom(street, date);
            IsolationPlaceStatisticsCount data = new IsolationPlaceStatisticsCount();
            data.setName(date);
            data.setCount(String.valueOf(personnelTotal));
            result.add(data);
        }
        return result;
    }

    @Override
    public Map<String, Integer> getRelieveData(String street) {
        Map<String,Integer> data = new HashMap<>();
        data.put("today",isolationPlaceStatisticsMapper.getRelieveToday(street));
        data.put("tomorrow",isolationPlaceStatisticsMapper.getRelieveTomorrow(street));
        return data;
    }

    @Override
    public Map<String, Integer> getHistoryEqData(String street, String date) {
        Map<String,Integer> data = new HashMap<>();
        //历史数据
        Integer allCount = isolationPlaceStatisticsMapper.getHistoryEqData(date,street);
        data.put("allCount",allCount);

        //阳性数据
        Integer allPositive = isolationPlaceStatisticsMapper.getHistoryEqPositiveData(date,street);
        data.put("allPositive",allPositive);

        //复阳数据
        Integer  afterPositive = isolationPlaceStatisticsMapper.getAfterPositiveEq(date,street);
        data.put("afterPositive",afterPositive);
        return data;
    }

    @Override
    public Object getPersonnelType(StatisticsSearchPersonnelType params) {
        List list;
        if (params.isPage()) {
            PageHelper.startPage(params.getPageNum(), params.getPageSize());
            list = isolationPlaceStatisticsMapper.getPersonnelType(params);
            PageInfo<Object> page = new PageInfo(list);
            return new IPageInfo(page);
        } else {
            list = isolationPlaceStatisticsMapper.getPersonnelType(params);
            return list;
        }
    }

    @Override
    public Map<String, Object> getIsolationPlaceInfo(String placeName) {
        Map<String,Object> data = new HashMap<>();
        //获取人员信息
        String placeId = isolationPlaceMapper.getPlaceId(placeName);
        data.put("personnel",isolationPlaceStatisticsMapper.getPersonnel(placeId));
        data.put("room",isolationPlaceStatisticsMapper.getRoom(placeId));
        data.put("placeInfo",isolationPlaceStatisticsMapper.getThreshold(placeId));
        return data;
    }
}
